package nwpu.lizhiwen.system;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class Activity {
    private String id;
    private String name;
    private String description;
    private String site;
    private Date startime;
    private Date endtime;
    private Organizer organizers[]= new Organizer[10];
    private int organizerNumber = 0;
    private Teacher teachers[] = new Teacher[10];
    private int teacherNumber = 0;
    private Team teams[] = new Team[10];
    private int teamNumber = 0;

    public Activity(String id, String name, String description, String site, String star, String end) {
        String str1 = star ;
        String str2 = end  ;
        SimpleDateFormat sdf1 = new SimpleDateFormat("MM.dd.hh");
        Date startime = null;
        Date endtime = null;

        try {
            startime = sdf1.parse(str1+ ".0");
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }


        try {
            endtime = sdf1.parse(end+ ".24");
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }


        this.id = id;
        this.name = name;
        this.description = description;
        this.site = site;
        this.startime = startime;
        this.endtime = endtime;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public String getSite() {
        return site;
    }

    public void setSite(String site) {
        this.site = site;
    }

    public Date getStartime() {
        return startime;
    }

    public void setStartime(String startime) {
        String str1 = startime ;
        SimpleDateFormat sdf1 = new SimpleDateFormat("MM.dd.hh");
        Date time = null;
        try {
            time = sdf1.parse(str1+ ".0");
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
        this.startime = time;
    }

    public Date getEndtime() {
        return endtime;
    }

    public void setEndtime(String endtime) {
        String str1 = endtime ;
        SimpleDateFormat sdf1 = new SimpleDateFormat("MM.dd.hh");
        Date time = null;
        try {
            time = sdf1.parse(str1+ ".0");
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
        this.endtime = time;
    }

    //以下的add/del方法逻辑相同，只对一处注释
    //该方法用于向活动中添加主办方
    public void addOrganizer(Organizer organizer){
        if (organizerNumber == organizers.length){
            return ;
        }
        organizers[organizerNumber++] = organizer;
        System.out.println("添加成功");
    }
   //该方法用于将活动中的主办方删除
    public boolean delOrganizer(String delId){
        int index = -1;
        //调用organizers类的getId方法与传进来的delId一一对比
        for (int i = 0;i <organizerNumber; i++){
            if (delId .equals(organizers[i].getId()) ){
                index = i;
            }
        }
        //若该活动下没该主办方就返回false
        if (index == -1){
            return false;
        }
        //若找到该主办方则将该元素之后的元素整体前移一位，并将移动后的最后一位元素，也就是要删除的元素赋为空
        for (int i = index; i < organizerNumber-1; i++){
            organizers[i] = organizers[i+1];
        }
        organizers[organizerNumber-1] = null;
        //数组长度减一
        organizerNumber--;
        return true;
    }

    public void addTeacher(Teacher teacher){
        if (teacherNumber == teachers.length){
            return ;
        }
        teachers[teacherNumber++] = teacher;
        System.out.println("添加成功");
    }
    public boolean delTeacher(String delId){
        int index = -1;
        for (int i = 0;i <teacherNumber; i++){
            if (delId .equals(teachers[i].getId()) ){
                index = i;
            }
        }
        if (index == -1){
            return false;
        }
        for (int i = index; i < teacherNumber-1; i++){
            teachers[i] = teachers[i+1];
        }
        teachers[teacherNumber-1] = null;
        teacherNumber--;
        return true;
    }

    public void addTeam(Team team){
        if (teamNumber == teams.length){
            return ;
        }
        teams[teamNumber++] = team;
        System.out.println("添加成功");
        team.count1();
    }
    public boolean delTeam(String delId){
        int index = -1;
        for (int i = 0;i <teamNumber; i++){
            if (delId .equals(teams[i].getTeamId()) ){
                index = i;
            }
        }
        if (index == -1){
            return false;
        }
        for (int i = index; i < teamNumber-1; i++){
            teams[i] = teams[i+1];
        }
        teams[index].count2();
        teams[teamNumber-1] = null;
        teamNumber--;
        return true;
    }

    //此处三个方法逻辑相同
    //该方法传出此activity类包含的所有organizers
    public Organizer[] toStringOrganizer() {
        return organizers ;
    }
    public Teacher[] toStringTeacher() {
        return teachers ;
    }
    public Team[] toStringTeam() {
        return teams ;
    }

    @Override
    public String toString() {
        Date date1 = startime;
        Date date2 = endtime;
        SimpleDateFormat sdf = new SimpleDateFormat("MM月dd日");
        String star = sdf.format(date1);
        String end = sdf.format(date2);
        return id + "_" + name + "_" + description + "_" + site + "_" + star + "_" + end;
       }

}
